package se.ramfelt.psnfriends;

import se.ramfelt.psn.model.Friend;
import se.ramfelt.psn.model.TrophySummary;

import com.flurry.android.FlurryAgent;
import com.github.droidfu.imageloader.ImageLoader;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

public class FriendDetailDialog extends Dialog {

    private final Friend friend;

    protected FriendDetailDialog(Friend friend, Context context) {
        super(context);
        this.friend = friend;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        requestWindowFeature(Window.FEATURE_NO_TITLE);        
        setContentView(R.layout.friend_detail);
//        setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.friend);
//        setTitle(friend.getOnlineId());

        TrophySummary trophies = friend.getTrophySummary();
        if (trophies == null) {
            trophies = new TrophySummary();
        }
        setText(R.id.onlineid, friend.getOnlineId());
        setText(R.id.platinum, R.string.trophies_platinum, trophies.getPlatinum());
        setText(R.id.gold, R.string.trophies_gold, trophies.getGold());
        setText(R.id.silver, R.string.trophies_silver, trophies.getSilver());
        setText(R.id.bronze, R.string.trophies_bronze, trophies.getBronze());
        setText(R.id.level, R.string.trophies_level, trophies.getLevel());
        setText(R.id.count, R.string.trophies_count, trophies.getCount());
        
        ImageView view = (ImageView) findViewById(R.id.icon);
        if (friend.getAvatarUrl() != null) {
            ImageLoader.start(friend.getAvatarUrl(), view);
        }
//        fl.add(view, new LayoutParams(FILL_PARENT, WRAP_CONTENT))
    }
    private void setText(int textviewId, int textresourceId, int number) {
        setText(textviewId, String.format(getContext().getString(textresourceId), number));
    }
    private void setText(int textviewId, String textresource) {
        TextView view = (TextView) findViewById(textviewId);
        view.setText(textresource);
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        FlurryAgent.onStartSession(getContext(), Flurry.KEY);
    	FlurryAgent.onEvent("view.trophies");
    }
    @Override
    protected void onStop() {
        super.onStop();
        FlurryAgent.onEndSession(getContext());
    }
}
